********************************************************************************
********************************************************************************
** Robustness analysis 4
** Subject-specific private school learning premiums
********************************************************************************
********************************************************************************

** Load data
use "$dataraw_path\data_ftna_publication.dta", clear


** Subject-specific analysis
foreach var of varlist kiswahili_psle_num-science_psle_num {
	* Peers FTNA
	bys school_id year: egen temp1 = sum(`var')
	gen temp2 = temp1 - `var'
	bys school_id year: egen temp3 = count(`var')
	gen `var'_peers = temp2 / (temp3-1) if temp3>4
	bys school_id_psle school_id year: egen temp4 = sum(`var') // only peers not going to the same primary school
	bys school_id_psle school_id year: egen temp5 = count(`var') // only peers not going to the same primary school
	gen `var'_peers2 = (temp1 - temp4) / (temp3 - temp5) // only peers not going to the same primary school
	
	egen `var'_peers_sd = std(`var'_peers) if sample==1
	egen `var'_peers_sd2 = std(`var'_peers2) if sample==1 // only peers not going to the same primary school
	
	gen temp6 = (`var'==4) if `var'!=.
	bys school_id year: egen temp7 = sum(temp6)
	gen `var'_peers_a = (temp7-temp6)/(temp3-1)
	
	drop temp1-temp3 temp4 temp5 temp6 temp7
	
	* Peers PSLE
	bys school_id_psle year: egen temp1 = sum(`var')
	gen temp2 = temp1 - `var'
	bys school_id_psle year: egen temp3 = count(`var')
	gen `var'_peers_psle = temp2 / (temp3-1) if temp3>4
	
	egen `var'_peers_psle_sd = std(`var'_peers_psle) if sample==1
	drop temp1 temp2 temp3
}

areg kiswahili_psle_num i.english_psle_num i.math_psle_num ///
	i.society_psle_num i.science_psle_num female i.year, ///
	a(school_id_psle) cl(school_id_psle)
predict kisw_psle_iv
gen kisw = kisw_psle_iv
areg english_psle_num i.kiswahili_psle_num i.math_psle_num ///
	i.society_psle_num i.science_psle_num female i.year, ///
	a(school_id_psle) cl(school_id_psle)
predict engl_psle_iv
gen engl = engl_psle_iv
areg math_psle_num i.english_psle_num i.kiswahili_psle_num ///
	i.society_psle_num i.science_psle_num female i.year, ///
	a(school_id_psle) cl(school_id_psle)
predict math_psle_iv
gen math = math_psle_iv

foreach var of varlist kisw engl math {
	egen `var'_psle_iv2 = cut(`var'), at(0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7, ///
		0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.2,2.3,2.4, ///
		2.5,2.6,2.7,2.8,2.9,3.0,3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8,3.9,4.0,4.1, ///
		4.2,4.3,4.4,4.5) icodes
	egen group_id_`var'_iv = group(school_id_psle `var'_psle_iv2 year)
	duplicates tag group_id_`var'_iv, g(group_duplicates_`var'_iv)
	replace group_duplicates_`var'_iv = . if group_duplicates_`var'_iv>1000
	replace group_id_`var'_iv = . if group_duplicates_`var'_iv==.
	drop `var' `var'_psle_iv2 group_duplicates_`var'_iv
}

foreach var of varlist kiswahili_psle_num english_psle_num math_psle_num ///
kisw_psle_iv engl_psle_iv math_psle_iv kiswahili_ftna_num english_ftna_num ///
math_ftna_num {
	egen `var'_sd = std(`var') if sample==1
}

rename kiswahili_psle_num_sd temp_score
rename kiswahili_psle_num_peers_sd temp_score2
rename kiswahili_psle_num_peers_a temp_score3
eststo rob4_1: areg kiswahili_ftna_num_sd temp_score private female ///
	uncommon_name temp_score2 peers_fail_share temp_score3 i.year ///
	if sample==1, a(school_id_psle) cl(school_id)
rename temp_score kiswahili_psle_num_sd
rename kisw_psle_iv_sd temp_score
eststo rob4_2: areg kiswahili_ftna_num_sd temp_score private female ///
	uncommon_name temp_score2 peers_fail_share temp_score3 i.year ///
	if sample==1, a(school_id_psle) cl(school_id)
rename temp_score kisw_psle_iv_sd
rename temp_score2 kiswahili_psle_num_peers_sd
rename temp_score3 kiswahili_psle_num_peers_a

rename english_psle_num_sd temp_score
rename english_psle_num_peers_sd temp_score2
rename english_psle_num_peers_a temp_score3
eststo rob4_3: areg english_ftna_num_sd temp_score private female ///
	uncommon_name temp_score2 peers_fail_share temp_score3 i.year ///
	if sample==1, a(school_id_psle) cl(school_id)
rename temp_score english_psle_num_sd
rename engl_psle_iv_sd temp_score
eststo rob4_4: areg english_ftna_num_sd temp_score private female ///
	uncommon_name temp_score2 peers_fail_share temp_score3 i.year ///
	if sample==1, a(school_id_psle) cl(school_id)
rename temp_score engl_psle_iv_sd
rename temp_score2 english_psle_num_peers_sd
rename temp_score3 english_psle_num_peers_a

rename math_psle_num_sd temp_score
rename math_psle_num_peers_sd temp_score2
rename math_psle_num_peers_a temp_score3
eststo rob4_5: areg math_ftna_num_sd temp_score private female ///
	uncommon_name temp_score2 peers_fail_share temp_score3 i.year ///
	if sample==1, a(school_id_psle) cl(school_id)
rename temp_score math_psle_num_sd
rename math_psle_iv_sd temp_score
eststo rob4_6: areg math_ftna_num_sd temp_score private female ///
	uncommon_name temp_score2 peers_fail_share temp_score3 i.year ///
	if sample==1, a(school_id_psle) cl(school_id)
rename temp_score math_psle_iv_sd
rename temp_score2 math_psle_num_peers_sd
rename temp_score3 math_psle_num_peers_a

rename kiswahili_psle_num_peers_sd temp_score2
rename kiswahili_psle_num_peers_a temp_score3
eststo rob4_7: areg kiswahili_ftna_num_sd private female uncommon_name ///
	temp_score2 peers_fail_share temp_score3 ///
	if group_id_kisw_iv!=., cl(school_id) a(group_id_kisw_iv)
rename temp_score2 kiswahili_psle_num_peers_sd
rename temp_score3 kiswahili_psle_num_peers_a

rename english_psle_num_peers_sd temp_score2
rename english_psle_num_peers_a temp_score3
eststo rob4_8: areg english_ftna_num_sd private female uncommon_name ///
	temp_score2 peers_fail_share temp_score3 ///
	if group_id_engl_iv!=., cl(school_id) a(group_id_engl_iv)
rename temp_score2 english_psle_num_peers_sd
rename temp_score3 english_psle_num_peers_a

rename math_psle_num_peers_sd temp_score2
rename math_psle_num_peers_a temp_score3
eststo rob4_9: areg math_ftna_num_sd private female uncommon_name ///
	temp_score2 peers_fail_share temp_score3 ///
	if group_id_math_iv!=., cl(school_id) a(group_id_math_iv)
rename temp_score2 math_psle_num_peers_sd
rename temp_score3 math_psle_num_peers_a

* Output
esttab rob4_1 rob4_2 rob4_3 rob4_4 rob4_5 rob4_6 using ///
"$out_path\tablea10_part1.tex", replace se stats(N r2, fmt(%12.3gc) ///
labels("\(N\)" "\(R^2\)")) compress nomtitles ///
starlevels("" 0.01) substitute(\_ _) b(3) ///
/*KEEP*/k(private temp_score female uncommon_name temp_score2 ///
peers_fail_share temp_score3) ///
/*ORDER*/o(private temp_score female uncommon_name temp_score2 ///
peers_fail_share temp_score3) ///
/*LABELS*/varl(temp_score "\$\textit{PSLE score}$" private "\$Private_s$" ///
female "\$Female$" uncommon_name "\$\textit{Uncommon name}$" ///
temp_score2 "\$\textit{Peers PSLE}_{s}$" ///
peers_fail_share "\$\textit{Peers failed}_{s}$" ///
temp_score3 "\$\textit{Peers with A}_{s}$" ///
/*2016.year "\$Cohort16$" ///
2017.year "\$Cohort17$" _cons "\$Constant$"*/)

esttab rob4_7 rob4_8 rob4_9 using "$out_path\tablea10_part2.tex", replace se ///
stats(N r2, fmt(%12.3gc) labels("\(N\)" "\(R^2\)")) compress nomtitles ///
starlevels("" 0.01) substitute(\_ _) b(3) ///
/*KEEP*/k(private female uncommon_name temp_score2 peers_fail_share ///
temp_score3) ///
/*ORDER*/o(private female uncommon_name temp_score2 _cons) ///
/*LABELS*/varl(private "\$Private_s$" female "\$Female$" ///
uncommon_name "\$\textit{Uncommon name}$" ///
temp_score2 "\$\textit{Peers PSLE}_{s}$" ///
peers_fail_share "\$\textit{Peers failed}_{s}$" ///
temp_score3 "\$\textit{Peers with A}_{s}$")

restore
